package com.sky.mapper;

import com.sky.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;

@Mapper
public interface UserMapper {

    // 根据openid查询用户
    @Select("select * from user where openid = #{openid}")
    User getByOpenid(String openid);

    // 新用户注册
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user (openid,create_time,name,sex,phone,id_number,avatar) " +
            "values " +
            "(#{openid},#{createTime},#{name},#{sex},#{phone},#{idNumber},#{avatar})")
    void insert(User user);

    @Select("select * from user where id = #{id}")
    User getById(Long userId);

    // 统计截止到某天的用户数量
    @Select("select count(id) from user where create_time < #{endTime}")
    Integer getSumUserCount(LocalDateTime endTime);

    // 统计一段时间内新增的用户数量
    @Select("select count(id) from user where create_time >= #{beginTime} and create_time < #{endTime}")
    Integer getNewUserCount(LocalDateTime beginTime, LocalDateTime endTime);
}
